package cn.iotab.iotkit.server.demo.device.handler.firmware;

import cn.iotab.iotkit.transport.api.access.device.listener.firmware.DeviceRequestFirmwareListener;
import cn.iotab.iotkit.transport.api.access.model.custom.firmware.RequestFirmwareMessage;
import cn.iotab.iotkit.transport.api.access.model.custom.firmware.RequestFirmwareMessageReply;
import cn.iotab.iotkit.transport.api.access.model.message.DeviceMessageReply;
import cn.iotab.iotkit.transport.api.common.util.JsonUtil;
import com.google.common.util.concurrent.Futures;
import com.google.common.util.concurrent.ListenableFuture;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 设备主动检查升级请求，返回固件信息
 * <pre>
 *     请求：
 * {
 *     "id":"123",
 *     "ver":"1.0",
 *     "ack":1,
 *     "type":"firmware.request",
 *     "param":{
 *         "currentVersion":"1.2.3",
 *         "requestVersion":null
 *     }
 * }
 *
 *    响应：
 * {
 *     "code":200,
 *     "id":"123",
 *     "data":{
 *         "size":5391,
 *         "sign":"12ea4eb90cd22f5d",
 *         "firmwareId":"1234234",
 *         "version":"1.2.1",
 *         "url":"http://firmware.iotab.cn/aaa/bbb/download.bin",
 *         "signMethod":"md5"
 *     }
 * }
 * </pre>
 */
@Slf4j
@Component
public class MyDeviceRequestFirmwareListener extends DeviceRequestFirmwareListener {
    @Override
    protected ListenableFuture<DeviceMessageReply> doProcess(RequestFirmwareMessage message) {
        log.info("收到设备请求固件信息：{}", JsonUtil.toJsonString(message));
        //
        RequestFirmwareMessageReply reply = new RequestFirmwareMessageReply();
        reply.from(message);
        reply.setSuccess();
        reply.setUrl("http://firmware.iotab.cn/aaa/bbb/download.bin");
        reply.setVersion("1.2.1");
        reply.setSignMethod("md5");
        reply.setSign("12ea4eb90cd22f5d");
        reply.setFirmwareId("1234234");
        reply.setSize(5391);
        //
        return Futures.immediateFuture(reply);
    }
}
